package com.cjj2016.cjj.wandoujiaanimationdemo;

import android.animation.Animator;
import android.animation.ValueAnimator;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.ViewTreeObserver;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;

import com.cjj2016.cjj.wandoujiaanimationdemo.utils.DisplayUtil;

public class DetailActivity extends AppCompatActivity {
    private static final String TAG = "DetailActivity";
    private int mImageId;

    private String mAppName;

    private int itemViewMarginTop;

    private LinearLayout llContent;

    int rate=0;


    ScrollView scrollview;
    int mScrollViewWidth;
    int mScrollViewheight;

    ImageView iconImageView;
    int mIconImgViewWidth;
    int mIconImgViewHeight;

    int mIconImgEndLeftNum;
    int mIconImgEndTopNum;


    int mLLContentWidth;
    int mLLContentHeight;

    LinearLayout llBottom;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //去掉状态栏
        DisplayUtil.hideStatusBar(getWindow());
        setContentView(R.layout.activity_detail);

        Intent intent =getIntent();
        itemViewMarginTop = intent.getIntExtra("ItemViewMarginTop",0);
        mImageId = intent.getIntExtra("imageRid",0);
        mAppName = intent.getStringExtra("appName");


        scrollview = (ScrollView) findViewById(R.id.scrollview);

        scrollview.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {

                mScrollViewWidth = scrollview.getMeasuredWidth();
                mScrollViewheight = scrollview.getMeasuredHeight();

                mLLContentWidth = llContent.getMeasuredWidth();
                mLLContentHeight = llContent.getMeasuredHeight();

                mIconImgViewWidth = iconImageView.getMeasuredWidth();
                mIconImgViewHeight = iconImageView.getMeasuredHeight();


                llContent.layout(0,itemViewMarginTop,mLLContentWidth,itemViewMarginTop+mLLContentHeight);
            }
        });


        iconImageView = (ImageView) findViewById(R.id.iv_detail);
        iconImageView.setImageResource(mImageId);



        llContent = (LinearLayout) findViewById(R.id.ll_content);



        llBottom = (LinearLayout)  findViewById(R.id.ll_bottom);

        Button btnInstall= (Button) findViewById(R.id.change);

//        btnInstall.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                rate +=10;
//                scrollview.layout(0,rate,mScrollViewWidth, rate + mScrollViewheight);
//
//            }
//        });

        initAnimationData();
    }

    private void initAnimationData() {
        //llcontent最终的top值
        final int mllContentEndTopNum = itemViewMarginTop-getResources().getDimensionPixelOffset(R.dimen.detail_bar_view_height);
        //llcontent最终的bottom值
        final int mllContentEndBottomNum =mScrollViewheight - llContent.getBottom();

        //图标最终位置的left值( （屏幕的宽度-图标自己的宽度）/2-图标的marginlef值 )
         mIconImgEndLeftNum = (DisplayUtil.getScreenWidth(DetailActivity.this)-iconImageView.getWidth())/2 - getResources().getDimensionPixelOffset(R.dimen.icon_margin);
        //图标最终位置的top值
         mIconImgEndTopNum = getResources().getDimensionPixelOffset(R.dimen.iconimg_view_endTop);


        ValueAnimator valueAnimator = ValueAnimator.ofFloat(0,1).setDuration(400);
        valueAnimator.setStartDelay(100);
        valueAnimator.start();
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float ratio = (float) animation.getAnimatedValue();

                //llcontent的top偏移量
                int llCotentTopOffset = (int) (ratio * mllContentEndTopNum);
                //llcontent的bottom偏移量
                int llCotentBottomOffset = (int) (ratio * mllContentEndBottomNum);

                llContent.layout(0,itemViewMarginTop-llCotentTopOffset,mLLContentWidth, itemViewMarginTop-llCotentTopOffset+mLLContentHeight);

                //图标Left的偏移量
                int iconImgLeftOffset = (int) (ratio * mIconImgEndLeftNum);
                //图标Top的偏移量
                int iconImgTopOffset = (int) (ratio * mIconImgEndTopNum);


                iconImageView.layout(iconImgLeftOffset,iconImgTopOffset,iconImgLeftOffset +mIconImgViewWidth,iconImgTopOffset+mIconImgViewHeight);
               // Log.i(TAG, "onAnimationUpdate: value="+ratio);
            }
        });
        valueAnimator.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {

            }

            @Override
            public void onAnimationEnd(Animator animation) {
               // Log.i(TAG, "onAnimationEnd: exec");
               // llBottom.setVisibility(View.VISIBLE);
            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });



    }




}
